package thread;

class TicketTask implements Runnable {
    //临界资源（多线程共享资源）
     int count = 100;
    public void run() {
        while(true) {
            synchronized (this) { //同步代码块，这里的this可以看成是对象锁（同步锁）
                if (count <= 0) break;
                System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + count);
                count--;
            }

        }
    }
}
/**
 * @Author Justin Parker
 * @Date 2024/7/20 0020 10:14
 * @Version 1.0
 */
public class TicketDemo01 {
    public static void main(String[] args) {
        TicketTask ticketTask = new TicketTask();
        Thread thread1 = new Thread(ticketTask,"窗口1");
        Thread thread2 = new Thread(ticketTask,"窗口2");
        Thread thread3 = new Thread(ticketTask,"窗口3");
        thread1.start();
        thread2.start();
        thread3.start();


    }

}
